﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Helpers;
using AutoR.Messages;
using AutoR.Models;
using AutoR.Services;
using LiteDB;
using Prism.Mvvm;
using Xamarin.Forms;

namespace AutoR.Tasks
{
	// Token: 0x02000102 RID: 258
	public class ExploreTask : BindableBase
	{
		// Token: 0x1700017A RID: 378
		// (get) Token: 0x06000575 RID: 1397 RVA: 0x000408F6 File Offset: 0x0003EAF6
		protected GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x1700017B RID: 379
		// (get) Token: 0x06000576 RID: 1398 RVA: 0x0005634E File Offset: 0x0005454E
		// (set) Token: 0x06000577 RID: 1399 RVA: 0x00056358 File Offset: 0x00054558
		public int Id
		{
			[CompilerGenerated]
			get
			{
				return this.<Id>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Id>k__BackingField == value)
				{
					return;
				}
				this.<Id>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Id);
			}
		}

		// Token: 0x1700017C RID: 380
		// (get) Token: 0x06000578 RID: 1400 RVA: 0x00056385 File Offset: 0x00054585
		// (set) Token: 0x06000579 RID: 1401 RVA: 0x00056390 File Offset: 0x00054590
		public string Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<Type>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x1700017D RID: 381
		// (get) Token: 0x0600057A RID: 1402 RVA: 0x000563C1 File Offset: 0x000545C1
		// (set) Token: 0x0600057B RID: 1403 RVA: 0x000563CC File Offset: 0x000545CC
		public bool Enabled
		{
			[CompilerGenerated]
			get
			{
				return this.<Enabled>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Enabled>k__BackingField == value)
				{
					return;
				}
				this.<Enabled>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Enabled);
			}
		}

		// Token: 0x1700017E RID: 382
		// (get) Token: 0x0600057C RID: 1404 RVA: 0x000563F9 File Offset: 0x000545F9
		// (set) Token: 0x0600057D RID: 1405 RVA: 0x00056404 File Offset: 0x00054604
		[BsonIgnore]
		public Queue<int> CancelQueue
		{
			[CompilerGenerated]
			get
			{
				return this.<CancelQueue>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<CancelQueue>k__BackingField, value))
				{
					return;
				}
				this.<CancelQueue>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.CancelQueue);
			}
		}

		// Token: 0x1700017F RID: 383
		// (get) Token: 0x0600057E RID: 1406 RVA: 0x00056434 File Offset: 0x00054634
		// (set) Token: 0x0600057F RID: 1407 RVA: 0x0005643C File Offset: 0x0005463C
		[BsonIgnore]
		public int[] NextExplore
		{
			[CompilerGenerated]
			get
			{
				return this.<NextExplore>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<NextExplore>k__BackingField, value))
				{
					return;
				}
				this.<NextExplore>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.NextExplore);
			}
		}

		// Token: 0x06000580 RID: 1408 RVA: 0x0005646C File Offset: 0x0005466C
		public async Task Start()
		{
			for (int i = 0; i < this.CancelQueue.Count; i++)
			{
				int rid = this.CancelQueue.Dequeue();
				if (this.Game.PVEExplore.Any((UserExplore s) => s.exploreId == rid))
				{
					UserExplore ei = this.Game.PVEExplore.Single((UserExplore s) => s.exploreId == rid);
					ExploreVo exploreVo = await Runtime.Remote.ExploreCancel(rid);
					this.Game.PVEExplore = new ObservableCollection<UserExplore>(exploreVo.levels);
					this.NextExplore[ei.fleetId - 5] = 0;
					MessagingCenter.Send<LogMessage>(new LogMessage("[远征]取消" + ei.ExplorerName + "节点远征"), "Log");
					ei = null;
				}
			}
			ExploreTask.<>c__DisplayClass22_1 CS$<>8__locals2 = new ExploreTask.<>c__DisplayClass22_1();
			CS$<>8__locals2.i = 0;
			while (CS$<>8__locals2.i < 4)
			{
				if (this.Game.PVEExplore.Any((UserExplore s) => s.fleetId == CS$<>8__locals2.i + 5))
				{
					UserExplore ei = this.Game.PVEExplore.Single((UserExplore s) => s.fleetId == CS$<>8__locals2.i + 5);
					if (this.NextExplore[CS$<>8__locals2.i] == 0)
					{
						this.NextExplore[CS$<>8__locals2.i] = ei.exploreId;
					}
					if (DateTime.Now > ei.endTime)
					{
						FinishExplore finishExplore = await Runtime.Remote.ExploreResult(ei.exploreId);
						MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[远征]第{0}舰队{1}远征{2}成功", ei.fleetId, ei.exploreId, finishExplore.bigSuccess ? "大" : "")), "Log");
						this.Game.User.UpdateUserInfo(finishExplore.userResVo);
						this.Game.Package.UpdateUserPackage(finishExplore.packageVo);
						await this.Game.Delay(2000);
						ExploreVo exploreVo2 = await Runtime.Remote.ExploreStart(ei.fleetId, (this.NextExplore[CS$<>8__locals2.i] != ei.exploreId) ? this.NextExplore[CS$<>8__locals2.i] : ei.exploreId);
						foreach (UserExplore userExplore in exploreVo2.levels)
						{
							userExplore.endTime = userExplore.endTime.AddSeconds((double)new Random().Next(20, 200));
						}
						this.Game.PVEExplore = new ObservableCollection<UserExplore>(exploreVo2.levels);
						MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[远征]第{0}舰队开始{1}远征", ei.fleetId, ei.exploreId)), "Log");
					}
					ei = null;
				}
				else if (this.NextExplore[CS$<>8__locals2.i] != 0)
				{
					ExploreVo exploreVo3 = await Runtime.Remote.ExploreStart(CS$<>8__locals2.i + 5, this.NextExplore[CS$<>8__locals2.i]);
					foreach (UserExplore userExplore2 in exploreVo3.levels)
					{
						userExplore2.endTime = userExplore2.endTime.AddSeconds((double)new Random().Next(20, 200));
					}
					this.Game.PVEExplore = new ObservableCollection<UserExplore>(exploreVo3.levels);
					MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[远征]第{0}舰队开始{1}远征", CS$<>8__locals2.i + 5, this.NextExplore[CS$<>8__locals2.i])), "Log");
				}
				CS$<>8__locals2.i++;
			}
			CS$<>8__locals2 = null;
		}

		// Token: 0x06000581 RID: 1409 RVA: 0x000564B1 File Offset: 0x000546B1
		public ExploreTask()
		{
			this.<Id>k__BackingField = 2;
			this.<Type>k__BackingField = "ExploreTask";
			this.<CancelQueue>k__BackingField = new Queue<int>();
			this.<NextExplore>k__BackingField = new int[4];
			base..ctor();
		}
	}
}
